package com.updesignsz.admin.mapper;

import com.updesignsz.commom.entity.News;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 新闻
 * Creation date 2020-8-8 15:00:07
 */
@Repository
@Mapper
public interface NewsMapper {


    @Select({"<script>",
            "select `id`,`created_date`,`updated_date`, `type`, `title`, `cover`, `cover_thumbnail`, `release_date`, `sequence`, `is_removed` from news ",
            "<where>",
            "<if test=\"type != null\" >AND type = #{type}</if>",
            "</where>",
            "order by sequence desc, created_date desc",
            "</script>"
    })
    @Results({
            @Result(property = "typeEntity", column = "type", many = @Many(select = "com.updesignsz.admin.mapper.TypeMapper.getById"))
    })
    List<News> listAll(News type);


    @Select("select * from news where id = #{id}")
    News getById(@Param("id") Integer id);

    @Insert({
            "insert into news(title, `type`, cover, cover_thumbnail, content, release_date, sequence, is_removed, created_date, updated_date) ",
            "values (#{title},#{type},#{cover},#{coverThumbnail},#{content},#{releaseDate},1,0,NOW(),NOW())"
    })
    @Options(useGeneratedKeys = true, keyColumn = "id")
    void saveNews(News type);


    @Update({"<script>",
            "update news <set>",
            "<if test=\"title != null\" >title = #{title},</if>",
            "<if test=\"type != null\" >type = #{type},</if>",
            "<if test=\"cover != null\" >cover = #{cover},</if>",
            "<if test=\"coverThumbnail != null\" >cover_thumbnail = #{coverThumbnail},</if>",
            "<if test=\"content != null\" >content = #{content},</if>",
            "<if test=\"releaseDate != null\" >release_date = #{releaseDate},</if>",
            "<if test=\"sequence != null\" >sequence = #{sequence},</if>",
            "updated_date = NOW()",
            "</set> where id = #{id}",
            "</script>"
    })
    void updateNews(News type);

    @Delete("delete from news where id = #{id}")
    void deleteNews(@Param("id") Integer id);
}